var ScrambledEggs;
(function (ScrambledEggs) {
    (function (Modules) {
        var Birth = (function () {
            function Birth() {
            }
            Birth.prototype.initialize = function (gameState, onUpdate) {
                this._onUpdate = onUpdate;
            };
            Birth.prototype.update = function (gameState, triggerSource) {
                var me = this;
                var state = gameState.State;
                if(state.Gender != ScrambledEggs.Gender.Unknown || state.Phase > ScrambledEggs.GamePhase.Fetus) {
                    $("html").addClass("born");
                    $("#world").hide();
                    $("#heartbeat").hide();
                    $("#genderSelection").remove();
                    return;
                }
                $("html").removeClass("born");
                $("#heartbeat").show();
                var setBg = function (color, callback) {
                    $("html").animate({
                        backgroundColor: color
                    }, 1300, callback);
                };
                setBg("#220000", function () {
                    setBg("#110000", function () {
                        setBg("#150000", function () {
                            setBg("#110000", function () {
                                setBg("#220000", function () {
                                    setBg("#110000", function () {
                                        setBg("#150000", function () {
                                            setBg("#442222", function () {
                                                setBg("#663333", function () {
                                                    setBg("#333366", function () {
                                                    });
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
                this.heartbeatLoop(10);
                var heartbeat = $("#heartbeat");
                var heartbeatCloneInterval = setInterval(function () {
                    heartbeat.clone().removeAttr("id").insertAfter(heartbeat).fadeOut(2000, function () {
                        $(this).remove();
                    });
                }, 0);
                setTimeout(function () {
                    clearInterval(heartbeatCloneInterval);
                }, 15000);
                var windowHeight = $(window).innerHeight() - 20;
                var bodyWidth = $("body").innerWidth();
                setTimeout(function () {
                    console.warn("push!");
                    var world = $("#world").css({
                        marginTop: windowHeight / 2,
                        minHeight: 0
                    }).show();
                    world.animate({
                        minHeight: windowHeight / 7,
                        minWidth: windowHeight / 20,
                        marginTop: windowHeight * 7 / 8 / 2
                    }, 3000, "linear", function () {
                        world.animate({
                            minHeight: windowHeight,
                            minWidth: bodyWidth,
                            marginTop: 0
                        }, 9000, "easeInCubic", function () {
                            $("html").addClass("born");
                            $("#heartbeat").hide();
                            $("#world").hide();
                            $("#genderSelection").show();
                        });
                    });
                }, 0);
                $("#genderSelection button").click(function () {
                    var clickedButton = this;
                    $("#genderSelection").fadeOut("slow", function () {
                        state.Gender = $(clickedButton).is("#gender-male") ? ScrambledEggs.Gender.Male : ScrambledEggs.Gender.Female;
                        $("#nameSelection").submit(function () {
                            $(this).fadeOut("slow", function () {
                                state.Name = $("#name").val();
                                state.Phase = ScrambledEggs.GamePhase.Baby;
                                me._onUpdate();
                            });
                            return false;
                        }).fadeIn("slow", function () {
                            $("#name").focus();
                        });
                    });
                });
            };
            Birth.prototype.heartbeatLoop = function (loopCount) {
                if(loopCount <= 0) {
                    return;
                }
                var me = this;
                var halfWindowHeight = $(window).innerHeight() / 4 * 3;
                var halfWindowWidth = $(window).innerWidth() / 2;
                $("#heartbeat").css({
                    top: halfWindowHeight + "px",
                    left: "0px",
                    zIndex: 10,
                    position: "absolute"
                });
                me.showFlatLine(0, halfWindowWidth - halfWindowWidth / 8, function () {
                    me.showHeartBeat(halfWindowHeight, halfWindowWidth - halfWindowWidth / 8, halfWindowWidth + halfWindowWidth / 8, function () {
                        me.showFlatLine(halfWindowWidth + halfWindowWidth / 8, halfWindowWidth * 2 - 20, function () {
                            me.heartbeatLoop(loopCount - 1);
                        });
                    });
                });
            };
            Birth.prototype.showFlatLine = function (beginHorizontalPosition, endHorizontalPosition, callback) {
                $("#heartbeat").animate({
                    left: endHorizontalPosition + "px"
                }, 1000, "linear", callback);
            };
            Birth.prototype.showHeartBeat = function (verticalHeight, beginHorizontalPosition, endHorizontalPosition, callback) {
                var positionStep = (endHorizontalPosition - beginHorizontalPosition) / 8;
                $("#heartbeat").animate({
                    left: (beginHorizontalPosition + positionStep) + "px",
                    top: (verticalHeight + verticalHeight / 20) + "px"
                }, 125, "linear", function () {
                    $("#heartbeat").animate({
                        left: (beginHorizontalPosition + positionStep * 2) + "px",
                        top: verticalHeight + "px"
                    }, 125, "linear", function () {
                        $("#heartbeat").animate({
                            left: (beginHorizontalPosition + positionStep * 3) + "px"
                        }, 125, "linear", function () {
                            $("#heartbeat").animate({
                                left: (beginHorizontalPosition + positionStep * 4) + "px",
                                top: (verticalHeight + verticalHeight / 50) + "px"
                            }, 125, "linear", function () {
                                $("#heartbeat").animate({
                                    left: (beginHorizontalPosition + positionStep * 5) + "px",
                                    top: (verticalHeight - verticalHeight / 5) + "px"
                                }, 125, "linear", function () {
                                    $("#heartbeat").animate({
                                        left: (beginHorizontalPosition + positionStep * 6) + "px",
                                        top: (verticalHeight + verticalHeight / 5) + "px"
                                    }, 125, "linear", function () {
                                        $("#heartbeat").animate({
                                            left: (beginHorizontalPosition + positionStep * 7) + "px",
                                            top: verticalHeight + "px"
                                        }, 125, "linear", function () {
                                            $("#heartbeat").animate({
                                                left: (beginHorizontalPosition + positionStep * 8) + "px"
                                            }, 125, "linear", callback);
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            };
            return Birth;
        })();
        Modules.Birth = Birth;        
    })(ScrambledEggs.Modules || (ScrambledEggs.Modules = {}));
    var Modules = ScrambledEggs.Modules;
})(ScrambledEggs || (ScrambledEggs = {}));
